﻿jQuery(document).ready(function () {
    //initAutoActivities();

    if ($('.flot-line').length > 0) {
        $(function () {
            var updateInterval = 1000;
            var options = {
                series: { 
                    shadowSize: 2 
                },
                yaxis: { 
                    min: 0, 
                    max: 100 
                },
                xaxis: { 
                    show: false 
                },
                grid: {
                    borderWidth: 1,
                    //minBorderMargin: 20,
                    labelMargin: 10,
                    backgroundColor: {
                        colors: ["#fff", "#e4f4f4"]
                    }
                }
            };
            var ramData = [], cpuData = [], totalPoints = 100;

            
            function update() {
                $.ajax({
                    method: "GET",
                    url: "/Home/GetStaticData",
                    success: function (res) {
                        if (ramData.length > 0)
                            ramData = ramData.slice(1);
                        while (ramData.length < totalPoints) {
                            ramData.push(res.Ram);
                        }
                        var ramresult = [];
                        for (var i = 0; i < ramData.length; ++i)
                            ramresult.push([i, ramData[i]]);
                        

                        if (cpuData.length > 0)
                            cpuData = cpuData.slice(1);
                        while (cpuData.length < totalPoints) {
                            cpuData.push(res.Cpu);
                        }
                        var cpuresult = [];
                        for (var j = 0; j < cpuData.length; ++j)
                            cpuresult.push([j, cpuData[j]]);
                        
                        var plot = $.plot($(".flot-line"), [{
                            label: "CPU 0 %",
                            data: cpuresult,
                            lines: { show: true, fill: false },
                            points: { show: false },
                            color: '#fd6e58'
                        }, {
                            label: "RAM 0 %",
                            data: ramresult,
                            lines: { show: true, fill: false },
                            points: { show: false },
                            color: 'rgb(55,183,243)'
                        }], options);
                        
                        //plot.setData([{
                        //    label: "CPU " + res .Cpu+ " %",
                        //    data: cpuresult,
                        //    lines: { show: true, fill: false },
                        //    points: { show: false },
                        //    color: 'rgb(209,38,16)'
                        //},{
                        //    label: "RAM " + res.Ram + " %",
                        //    data: ramresult,
                        //    lines: { show: true, fill: false },
                        //    points: { show: false },
                        //    color: 'rgb(55,183,243)'
                        //    }
                        //]);
                        
                        //plot.draw();
                        var f = 1;
                        $('.legendLabel').each(function () {
                            if (f == 1) $(this).html("CPU " + res.Cpu + " %");
                            else $(this).html("RAM " + res.Ram + " %");
                            f++;
                        });
                        setTimeout(update, updateInterval);
                    },
                    error: function () {
                    }
                });
                
            }

            // This resize bind fixes live-data resize bug in jquery.flot.resize.min
            $(window).resize(function () {
                if ($(".flot-line").is(":visible")) {
                    plot.resize();
                    plot.setupGrid();
                    plot.draw();
                }
            });

            update();
        });
    }
});
function initAutoActivities() {
    var auto_refresh = setInterval(
       function () {
           loadNewActivities();
       }, 10000); // refresh every 10000 millisecon

}
function loadNewActivities() {
    $.ajax({
        method: "GET",
        url: "/Home/LoadNewActivities",
        data:{lastTime: $("#LastActivitiesTime").val()},
        success: function (data) {
            $("#LastActivitiesTime").remove();
            $("#lstActivities").prepend(data);
        },
        error: function () {
        }
    });
}
